package com.guch.help.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ProtectedSpUtils {
    private volatile static SharedPreferences mSharedPreferences;
    private static Context context;
    private volatile static String spName;
    private volatile static ProtectedSpUtils instance;

    public static final String DEEP_DEL_SP_NAME = "sp_deep_del";
    /**
     * 记录 是否已经使用过
     */
    public static final String DEEP_DEL_TIME_SP_KEY = "sp_deep_del_times";
    /**
     * 记录 有效期-日期
     */
    public static final String VALIDITY_TIME_SP_KEY = "sp_validity_time";
    /**
     * 上一次有效期截止时间
     */
    public static final String VALIDITY_TIME = "2024-11-18";

    /**
     * 记录 是否是第一次进入
     */
    public static final String DEEP_DEL_FIRST_ENTER_KEY = "sp_deep_del_first_enter";

    /**
     * 需在Application的onCreate方法中调用
     *
     * @param con
     */
    public static void initContext(Context con) {
        context = con;
    }

    public static ProtectedSpUtils getInstance(String SP_NAME) {
        if (instance == null) {
            synchronized (ProtectedSpUtils.class) {
                if (instance == null) {
                    instance = new ProtectedSpUtils();
                }
            }
        }
        synchronized (ProtectedSpUtils.class) {
            if (!TextUtils.equals(SP_NAME, spName)) {
                spName = SP_NAME;
                mSharedPreferences = context
                        .getSharedPreferences(spName, Context.MODE_PRIVATE);
            }
        }
        return instance;
    }

    /**
     * 清空保存在默认SharePreference下的所有数据
     */
    public void clear() {
        mSharedPreferences.edit().clear().commit();
    }

    /**
     * 保存字符串
     *
     * @return
     */
    public void putString(String key, String value) {
        mSharedPreferences.edit().putString(key, value).apply();
    }

    /**
     * 读取字符串
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }


    /**
     * 保存字符串
     *
     * @return
     */
    public void putStrArray(String key, List<String> data) {
        StringBuffer sb = new StringBuffer();
        if (data != null && data.size() != 0) {
            for (int i = 0; i < data.size(); i++) {
                sb.append(data.get(i));

                if (i != data.size() - 1) {
                    sb.append(",");
                }
            }
        }

        mSharedPreferences.edit().putString(key, sb.toString()).commit();
    }

    /**
     * 保存字符串
     *
     * @return
     */
    public void putStrArray(String key, String data, int max) {
        mSharedPreferences.getString(key, "");
        String str = mSharedPreferences.getString(key, "");
        try {
            List<String> list = getStrArray(key);

            if (str.contains(data)) {
                for (int i = 0; i < list.size(); i++) {
                    if (data.equals(list.get(i))) {
                        list.remove(i);
                        break;
                    }
                }
            }

            while (list.size() > max - 1) {
                list.remove(list.size() - 1);
            }

            list.add(0, data);

            putStrArray(key, list);
        } catch (Exception e) {
        }

    }

    /**
     * 读取字符串
     *
     * @param key
     * @return
     */
    public List<String> getStrArray(String key) {
        String data = mSharedPreferences.getString(key, "");
        List<String> result = new ArrayList<String>();
        try {
            String[] ss = data.split(",");

            for (String s : ss) {
                if (s != null && !s.equals(""))
                    result.add(s);
            }

        } catch (Exception e) {
        }
        return result;
    }

    /**
     * 读取字符串
     *
     * @param key
     * @return
     */
    public Set<String> getStringSet(String key) {
        return mSharedPreferences.getStringSet(key, null);
    }

    /**
     * 读取字符串
     *
     * @param key
     * @return
     */
    public void putStringSet(String key, Set<String> data) {
        mSharedPreferences.edit().putStringSet(key, data).apply();
    }

    /**
     * 读取字符串
     *
     * @param key
     * @return
     */
    public String getString(String key, String defaultsString) {
        return mSharedPreferences.getString(key, defaultsString);

    }

    /**
     * 保存整型值
     *
     * @return
     */
    public void putInt(String key, int value) {
        mSharedPreferences.edit().putInt(key, value).commit();
    }

    /**
     * 读取整型值
     *
     * @param key
     * @return
     */
    public int getInt(String key, int defInt) {
        return mSharedPreferences.getInt(key, defInt);
    }

    /**
     * 保存布尔值
     *
     * @return
     */
    public void putBoolean(String key, Boolean value) {
        mSharedPreferences.edit().putBoolean(key, value).commit();
    }

    public void putLong(String key, long value) {
        mSharedPreferences.edit().putLong(key, value).commit();
    }

    public long getLong(String key, long defLong) {
        return mSharedPreferences.getLong(key, defLong);
    }

    /**
     * t 读取布尔值
     *
     * @param key
     * @return
     */
    public boolean getBoolean(String key, boolean defValue) {
        return mSharedPreferences.getBoolean(key, defValue);

    }

    public void putFloat(String key, float value) {
        mSharedPreferences.edit().putFloat(key, value).commit();

    }

    public float getFloat(String key, float defValue) {
        return mSharedPreferences.getFloat(key, defValue);

    }

    /**
     * 获取空调滤芯健康值上次上报时间
     */
    public long getLastAirHealthReportTime(String key) {
        return mSharedPreferences.getLong(key, 0L);
    }

    /**
     * 设置空调滤芯健康值上报时间
     */
    public void putAirHealthReportTime(String key, long reportTime) {
        mSharedPreferences.edit().putLong(key, reportTime).commit();
    }

    /**
     * 移除字段
     *
     * @return
     */
    public void remove(String key) {
        mSharedPreferences.edit().remove(key).commit();
    }

    /**
     * 删除sp中所有数据
     */
    public void delete() {
        mSharedPreferences.edit().clear().commit();
    }
}
